package com.lantu.wisdom.controller;


import com.lantu.framework.common.utils.BeanMapper;
import com.lantu.framework.common.utils.ResponseWrapper;
import com.lantu.wisdom.common.utils.OpenIdUtil;
import com.lantu.wisdom.dto.AddUserDTO;
import com.lantu.wisdom.dto.vo.UserVO;
import com.lantu.wisdom.model.WUser;
import com.lantu.wisdom.service.WUserApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zch
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/wUser")
@Api(tags = {"用户信息相关"})
public class WUserController {

    @Autowired
    WUserApi userService;


    @ApiOperation(value = "获取用户手机号")
    @RequestMapping(value = "/wxDecrypt", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "encrypted", value = "encrypted", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "sessionKey", value = "sessionKey", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "iv", value = "iv", required = true, dataType = "String")}
    )
    public ResponseWrapper<String> getOpenId(@RequestParam("encrypted") String encrypted, @RequestParam("sessionKey") String sessionKey, @RequestParam("iv") String iv) {
       /* JSONObject jsonObject = OpenIdUtil.wxDecrypt(encrypted, sessionKey, iv);
        String phone = "";
        if (!StringUtils.isEmpty(jsonObject)) {
            phone = jsonObject.getString("phoneNumber");
        }*/
        return ResponseWrapper.success("");
    }

    @ApiOperation(value = "获取用户openId")
    @RequestMapping(value = "/openId/{code}", method = RequestMethod.GET)
    @ApiImplicitParam(paramType = "path", name = "code", value = "code", required = true, dataType = "String")
    public ResponseWrapper<Map<String,String>> getOpenId(@PathVariable String code) {
        Map<String,String> openId = OpenIdUtil.getOpenId(code);
        return ResponseWrapper.success(openId);
    }



    @ApiOperation(value = "添加用户接口")
    @RequestMapping(value = "/adduser", method = RequestMethod.POST)
    public ResponseWrapper addUser(@RequestBody @Validated AddUserDTO addUserDTO) {
        //SensitiveWordUtil.paramSensitiveWord(addUserDTO.paramSplicing());
        return userService.addUser(addUserDTO) ? ResponseWrapper.success() : ResponseWrapper.exception();
    }


    @ApiOperation(value = "获取用户详情")
    @RequestMapping(value = "/user/{openId}", method = RequestMethod.GET)
    @ApiImplicitParam(paramType = "path", name = "openId", value = "用户微信openId", required = true, dataType = "String")
    public ResponseWrapper<UserVO> addUser(@PathVariable String openId) {
        WUser userInfo = userService.getUserInfoByOpenId(openId);
        UserVO userVO = null;
        if (null != userInfo) {
            userVO = BeanMapper.map(userInfo, UserVO.class);
        }
        return ResponseWrapper.success(userVO);
    }


}

